/**
 * 根据游标查询符合的数据
 * @param { Object } db 
 * @param { String } storeName 
 * @param { Object } params 
 * @returns 
 */
export function getDataLikeCursor(db, storeName, params) {
    return new Promise((resolve, reject) => {
        const objectStore = db.transaction([storeName]).objectStore(storeName);
        const requset = objectStore.openCursor();

        const results = [];

        requset.onsuccess = function (event) {
            const cursor = event.target.result;
            if (cursor) {
                const value = cursor.value;
                console.log(value)
                if (value && filterStore(params,value)) {
                    results.push(value);
                }
                cursor.continue(); // 继续下一个记录
            } else {
                // 返回结果
                resolve(results);
            }
        }

        requset.onerror = function (event) {
            reject(error);
        }

    })
}


function filterStore(params, store) {
    const searchParams = Object.entries(params);    // 对象转数组 {key:value} => [[key,value]]
    return searchParams.find(([key, value]) => {
        return store[key].includes(value);
    })
}